//i商品分类全局组件的小仓库

import { defineStore } from 'pinia'
import {
  getPrimary,
  getSecondary,
  getReclassify,
  getPropertyData,
} from '@/api/product/attr/index.ts'

const useCategoryStore = defineStore('Category', {
  state: () => {
    return {
      primaryTitle: [], //一级分类
      secondaryTitle: [], //二级分类
      reclassifyTitle: [], //三级分类
      PropertyData: [], //属性数据
    }
  },
  actions: {
    /**
     * 获取一级分类标题
     * @returns
     */
    async getPrimaryStore() {
      const result: any = await getPrimary()
      if (result.code == 200) {
        this.primaryTitle = result.data
        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    },

    /**
     * 获取二级分类
     * @param id 一级分类id
     * @returns
     */
    async getSecondaryStore(id: number) {
      const result: any = await getSecondary(id)
      console.log(result)
      if (result.code == 200) {
        this.secondaryTitle = result.data
        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    },

    /**
     * 获取三级分类
     * @param id 一级分类id
     * @returns
     */
    async getReclassifyStore(id: number) {
      const result: any = await getReclassify(id)
      console.log(result)
      if (result.code == 200) {
        this.reclassifyTitle = result.data
        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    },

    /**
     * 获取属性数据
     * @param id 一级分类id
     * @returns
     */
    async getPropertyStore(id: number) {
      const result: any = await getPropertyData(id)
      if (result.code == 200) {
        this.PropertyData = result.data
        return 'ok'
      } else {
        return Promise.reject(new Error(result.message))
      }
    },
  },
  getters: {},
})

export default useCategoryStore
